package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.DeviceDTO;
import com.hksj.Intelligentmanagement.entity.DeviceEntity;
import com.hksj.Intelligentmanagement.mapper.DeviceMapper;
import com.hksj.Intelligentmanagement.vo.DeviceVo;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author zengke
 * @ClassName DeviceService
 * @Date 2023/3/14 9:58
 * @Version 1.0
 * @Description TODO
 */

@Service
public class DeviceService extends ServiceImpl<DeviceMapper, DeviceEntity> {

    //注入DozerBeanMapper用于Entity与DTO之间的copy
    @Autowired
    private Mapper mapper;

    //注入DeviceMapper对matt_device表进行增删改查的操作
    @Autowired
    private DeviceMapper deviceMapper;

    //新增设备
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createDevice(DeviceDTO deviceDTO){
        //将DeviceDTO 复制到 DeviceEntity
        DeviceEntity deviceEntity = mapper.map(deviceDTO, DeviceEntity.class);
        //新增记录
        deviceMapper.insert(deviceEntity);
        return Result.ok("新增设备成功");
    }

    //删除设备
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result deleteDevice(String id){
        //通过id查询在数据库中是否存在
        DeviceEntity deviceEntity = deviceMapper.selectById(id);

        //判断deviceEntity是否为空
        if (Objects.isNull(deviceEntity)){
            return Result.error("输入的id不存在,删除设备失败");
        }

        //删除记录---逻辑删除
        deviceMapper.deleteById(id);
        return Result.ok("删除设备成功");
    }

    //修改设备
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result updateDevice(DeviceDTO deviceDTO){

        //通过id查询在数据库中是否存在
        DeviceEntity deviceEntity = deviceMapper.selectById(deviceDTO.getId());

        if (Objects.isNull(deviceEntity)){
            return Result.error("输入的id不正确,更新设备失败");
        }
        //将DeviceUpdateDTO复制到 DeviceEntity
        deviceEntity= mapper.map(deviceDTO, DeviceEntity.class);

        //更新记录
        deviceMapper.updateById(deviceEntity);
        return Result.ok("更新设备成功");
    }

    //查询所有设备
    public IPage<DeviceVo> queryDeviceListByPage(DeviceDTO deviceDTO){

        //创建分页实例
        IPage<DeviceVo> page = new Page<>(deviceDTO.getPageNum(),deviceDTO.getPageSize());

        //分页查询
        List<DeviceVo> deviceVos = deviceMapper.queryDeviceListByPage(page, deviceDTO);

        page.setRecords(deviceVos);

        return page;
    }

    //通过设备id查询设备信息
    public Result queryDeviceByDeviceId(String deviceId){
        QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<DeviceEntity>().eq("device_id", deviceId);

        DeviceEntity deviceEntity = deviceMapper.selectOne(wrapper);

        return Result.ok(deviceEntity);
    }

}
